package team.dmh.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.dmh.wx.entity.Friend;
import team.dmh.wx.entity.User;
import team.dmh.wx.exception.ExceptionEnum;
import team.dmh.wx.exception.MyException;
import team.dmh.wx.mapper.FriendMapper;
import team.dmh.wx.service.IFriendService;
import team.dmh.wx.service.IUserService;
import team.dmh.wx.vo.FriendVO;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzk
 * @since 2018-10-15
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {

    @Autowired
    private IUserService userService;

    @Override
    public void check(String userId, String friendUsername) {
        User friendInfo = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, friendUsername));
        if (friendInfo == null) {
            throw new MyException(ExceptionEnum.USER_NOT_EXIST);
        }
        if (friendInfo.getId().equals(userId)) {
            throw new MyException(ExceptionEnum.NOT_YOURSELF);
        }
        Friend friend = baseMapper.selectOne(
                new LambdaQueryWrapper<Friend>()
                        .eq(Friend::getUserId, userId)
                        .eq(Friend::getFriendId, friendInfo.getId())
                        );
        if (friend != null) {
            throw new MyException(ExceptionEnum.ALREADY_FRIEND);
        }
    }

    @Override
    public Map<String, List<FriendVO>> getFriendVOMap(String userId) {
        List<FriendVO> friendVOList = baseMapper.getFriendVOList(userId);
        Map<String, List<FriendVO>> friendVOMap = new HashMap<>();
        Map<String, List<FriendVO>> sortedFriendVOMap = new LinkedHashMap<>();
        friendVOList.forEach(friendVO -> {
            try {
                //获取昵称首字母
                char c = PinyinHelper.getShortPinyin(friendVO.getFriendNickname()).toUpperCase().charAt(0);
                //A-Z
                if (c >= 65 && c <= 90) {
                    if (friendVOMap.containsKey(String.valueOf(c))) {
                        List<FriendVO> list = friendVOMap.get(String.valueOf(c));
                        list.add(friendVO);
                        friendVOMap.put(String.valueOf(c), list);
                    } else {
                        List<FriendVO> list = new ArrayList<>();
                        list.add(friendVO);
                        friendVOMap.put(String.valueOf(c), list);
                    }
                } else {
                    if (friendVOMap.containsKey("#")) {
                        List<FriendVO> list = friendVOMap.get("#");
                        list.add(friendVO);
                        friendVOMap.put("#", list);
                    } else {
                        List<FriendVO> list = new ArrayList<>();
                        list.add(friendVO);
                        friendVOMap.put("#", list);
                    }
                }
            } catch (PinyinException e) {
                e.printStackTrace();
            }
        });
        List<Map.Entry<String, List<FriendVO>>> sortedList = new ArrayList<>(friendVOMap.entrySet());
        //按升序排列
        sortedList.sort((o1, o2) -> {
            if (o1.getKey().equals("#")) {
                return 1;
            } else if (o2.getKey().equals("#")) {
                return -1;
            } else {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        sortedList.forEach(e->sortedFriendVOMap.put(e.getKey(),e.getValue()));
        return sortedFriendVOMap;
    }

    @Override
    public List<FriendVO> getFriendVOList(String userId) {
        return baseMapper.getFriendVOList(userId);
    }

    @Override
    public boolean isExist(String userId, String friendId) {
        return baseMapper.getFriend(userId, friendId)!=null?true:false;
    }

    @Override
    public FriendVO getFriendInfo(String userId, String friendId) {
        return baseMapper.getFriend(userId,friendId);
    }

}
